/**
 *
 * @description: 电影
 * @author: junyong.hong
 * @createTime: 2019/2/23
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const MovieSchema = new Schema({
    doubanId: String,
    title: String,
    director: String,
    language: String,
    country: String,
    summary: String,
    poster: String,
    genres: [String],
    year: Number,
    pv: {
        type: Number,
        default: 0
    },
    category: {
        type: ObjectId,
        ref: 'Category'
    },
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt: {
            type: Date,
            default: Date.now()
        }
    }
})

MovieSchema.pre('save', function (next) {
    // 如果是新增的
    if (this.isNew) {
        this.meta.createdAt = this.meta.updatedAt = Date.now()
    } else {
        this.meta.updatedAt = Date.now()
    }

    next()
})

const Movie = mongoose.model('Movie', MovieSchema)
